Skip to main content

Learn ✔

Concepts | LeetCode

  1. Introduction to Bits and Tricks
  2. Check if the i-th bit is Set or Not
    1. 14 March, 2026: 00.01.26 ✔
    2. 19 March, 2026: 00.01.12 ✔
  3. Check if a Number is Odd or Not
    1. 12 March, 2026: 00.01.44 ❌
      • Intitution is right.
      • Couldn't able to implement it.
    2. 19 March, 2026: 00.01.58 ✔
      • Put the braces properly
  4. Check if a Number is Power of 4 or Not
    1. 14 March, 2026: 00.07.41 ❌
      • Failed at edge cases
    2. 19 March, 2026: 00.04.05 ✔
      • Put the braces properly
  5. Count the Number of Set Bits
    1. 14 March, 2026: 00.22.46 ❌
      • Failed at optimization
    2. 19 March, 2026: 00.09.16 ❌
      • Failed at cycle counting
  6. Set/Unset the rightmost unset bit
    1. 14 March, 2026: 00.00.00 ❌
      • Failed at implementation
      • Intitution is right.
    2. 19 March, 2026: 00.01.12 ✔
  7. Swap Two Numbers
    1. 13 March, 2026: 00.00.00 ❌
      • Failed at implementation
    2. 19 March, 2026: 00.02.13 ✔
  8. Divide two numbers without multiplication and division
    1. 14 March, 2026: 00.00.00 ❌

      • Failed at implementation
    2. 19 March, 2026: 00.12.06 ❌

      • Failed at Optimization

      Bruteforce

      • Handle the edge cases first.

      • XOR operator is used to determine the sign of the result.

      • Handle the overflow when taking absolute values using llabs().

        abs(INT_MIN) causes integer overflow because INT_MAX = 2^31 - 1.

        Even though you declared x and y as long long, abs(dividend) is computed as an int before being assigned to long long. This can result in undefined behavior.

        Fix: Use long long before calling abs:

        long long x = llabs((long long)dividend);
        long long y = llabs((long long)divisor);

      Optimization

      • Instead of subtracting y one by one,
      • Double y each time until it becomes larger than x.